//
//  IGCommentCell.m
//  jingdongfang
//
//  Created by BOE on 2018/5/15.
//  Copyright © 2018年 BOE-SBG-CTO. All rights reserved.
//

#import "IGCommentCell.h"

static NSString *const kIGCommentCellID = @"kIGCommentCellID";
@interface IGCommentCell()

@property (nonatomic, strong) UIImageView *headerimgView;
@property (nonatomic, strong) UILabel *nameLabel;                             ///< 头像右侧的姓名
@property (nonatomic, strong) UILabel *dateLabel;                             ///< 头像右侧的日期

@property (nonatomic, strong) UIImageView *zanImgView;                        ///< 点赞-图
@property (nonatomic, strong) UILabel *zanNumLabel;                           ///< 点赞-Num
@property (nonatomic, strong) UIButton *zanButton;                            ///< 点赞-区域按钮

@property (nonatomic, strong) YYLabel *contentLabel;
@property (nonatomic, strong) NSMutableAttributedString *commentAttributedText;///< 评论富文本信息

@end
@implementation IGCommentCell

+ (IGCommentCell *)cellWithTableView:(UITableView *)tableView
{
    IGCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGCommentCellID];
    if (!cell) {
        cell = [[IGCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGCommentCellID];
        cell.contentView.backgroundColor = [UIColor c3Color];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.contentLabel];

        self.headerimgView = [[UIImageView alloc] init];
        self.headerimgView.layer.masksToBounds = YES;
        self.headerimgView.layer.cornerRadius = 15;
        [self.contentView addSubview:self.headerimgView];
        self.headerimgView.backgroundColor = [UIColor mainBackgroundColor];
        
        self.nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.nameLabel];
        self.nameLabel.numberOfLines = 1;
        self.nameLabel.textColor = [UIColor c4Color];
        self.nameLabel.font = [UIFont font18];
        
        self.dateLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.dateLabel];
        self.dateLabel.numberOfLines = 1;
        self.dateLabel.textColor = [UIColor c5Color];
        self.dateLabel.font = [UIFont font12];
        
        self.zanImgView = [UIImageView new];
        [self.contentView addSubview:self.zanImgView];
        self.zanImgView.image = [UIImage imageNamed:@"赞"];
        
        self.zanNumLabel = [UILabel new];
        [self.contentView addSubview:self.zanNumLabel];
        self.zanNumLabel.font = [UIFont font13];
        self.zanNumLabel.textColor = [UIColor c5Color];
        self.zanNumLabel.textAlignment = NSTextAlignmentRight;
        
        self.zanButton = [UIButton new];
        [self.contentView addSubview:self.zanButton];
        __weak typeof(self) weakSelf = self;
        [self.zanButton setTapActionWithBlock:^{
            if (weakSelf.likeBlock) {
                weakSelf.likeBlock();
            }
        }];
        
        // 评论内容
        self.contentLabel = [[YYLabel alloc] init];
        [self.contentView addSubview:self.contentLabel];
        self.contentLabel.backgroundColor = [UIColor clearColor];
        self.contentLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 5 * kGAP10 - 2*kGAP10;
        self.contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.displaysAsynchronously = YES;
        self.contentLabel.textColor = [UIColor c4Color];
        self.contentLabel.font = [UIFont font13];
        self.contentLabel.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressBtn)];
        [longPressGesture setDelegate:self];
        longPressGesture.minimumPressDuration=3;//默认0.5秒 表示最短长按的时间
        [self.contentLabel addGestureRecognizer:longPressGesture];
        
        [self.headerimgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kGAP15);
            make.top.mas_equalTo(kGAP20);
            make.width.height.mas_equalTo(40);
        }];
        
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.headerimgView.mas_right).offset(kGAP10);
            make.top.mas_equalTo(self.headerimgView.mas_top);
            make.height.mas_equalTo(18);
        }];
        
        [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.nameLabel.mas_left);
            make.bottom.mas_equalTo(self.headerimgView.mas_bottom);
            make.height.mas_equalTo(18);
        }];
        
        [self.zanImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nameLabel.mas_top);
            make.right.mas_equalTo(-15);
            make.width.height.mas_equalTo(18);
        }];
        
        [self.zanNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.zanImgView.mas_top);
            make.bottom.mas_equalTo(self.zanImgView.mas_bottom);
            make.right.mas_equalTo(self.zanImgView.mas_left).mas_offset(-8);
            make.width.mas_equalTo(70);
        }];
        
        [self.zanButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.zanNumLabel.mas_left);
            make.right.mas_equalTo(self.zanImgView.mas_right);
            make.top.mas_equalTo(self.zanImgView.mas_top);
            make.bottom.mas_equalTo(self.zanImgView.mas_bottom);
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.headerimgView.mas_bottom).offset(14);
            make.left.mas_equalTo(self.headerimgView.mas_right).offset(kGAP10);
            make.right.mas_equalTo(-kGAP15);
        }];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor mainBackgroundColor];
        [self.contentView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(1);
            make.bottom.mas_equalTo(0);
        }];
        
        self.backgroundColor = [UIColor clearColor];
        self.hyb_lastViewInCell = self.contentLabel;
        self.hyb_bottomOffsetToCell = 3.0;
    }
    return self;
}

- (void)configCellWithModel:(IGCommentBean *)model {
    //清空
    self.commentAttributedText = [[NSMutableAttributedString alloc] initWithString:@""];
    
    [self.headerimgView sd_setImageWithURL:[NSURL URLWithString:model.commentUserAvater] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
    self.headerimgView.userInteractionEnabled = YES;
    [self.headerimgView setTapActionWithBlock:^{
//        IntroViewController *introVC = [IntroViewController new];
//        introVC.u_id = model.commentUserId;
//        [UIViewController ig_pushViewController:introVC animated:YES];
    }];
    self.nameLabel.text = model.commentUserNick;
    self.dateLabel.text = [NSDate timestampToString:model.dateCreated Format:@"MM-dd HH:mm"];
    self.nameLabel.userInteractionEnabled = YES;
    [self.nameLabel setTapActionWithBlock:^{
//        IntroViewController *introVC = [IntroViewController new];
//        introVC.u_id = model.commentUserId;
//        [UIViewController ig_pushViewController:introVC animated:YES];
    }];
    
    UIFont *font13 = [UIFont font13];
    UIColor *color5 = [UIColor c5Color];
    if (![model.parentCommentUserId isEqualToString:@""] && model.parentCommentUserId != nil) {//二级回复
        NSMutableAttributedString *commentInfo = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@ %@：%@", NSLocalizedString(@"Reply",nil),model.parentCommentUserNick, model.content] attributes:@{NSFontAttributeName: font13}];
        
        NSString *language = [[NSUserDefaults standardUserDefaults] objectForKey:KLanguage];
        NSUInteger localAddress;
        if ([language containsString:@"zh-Hans"]) {
            localAddress = 3;
        } else {
            localAddress = 6;
        }
        [commentInfo yy_setTextHighlightRange:NSMakeRange(localAddress, model.parentCommentUserNick.length+1) color:color5 backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
//            IntroViewController *introVC = [IntroViewController new];
//            introVC.u_id = model.replyUserId;
//            [UIViewController ig_pushViewController:introVC animated:YES];
        }];
        
        [self.commentAttributedText appendAttributedString:commentInfo];
    } else {//直接回复
        NSMutableAttributedString *commentInfo = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@", model.content] attributes:@{NSFontAttributeName: font13}];
        
        [self.commentAttributedText appendAttributedString:commentInfo];
    }
    
    self.commentAttributedText.yy_lineSpacing = 5;
    self.commentAttributedText.yy_kern = [NSNumber numberWithFloat:.5];
    self.contentLabel.attributedText = self.commentAttributedText;
}

- (NSMutableAttributedString *)commentAttributedText {
    if (!_commentAttributedText) {
        _commentAttributedText = [NSMutableAttributedString new];
    }
    return _commentAttributedText;
}

- (void)longPressBtn {
    [self becomeFirstResponder];
    UIMenuItem *copyLink = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(copy:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:copyLink, nil]];
    [[UIMenuController sharedMenuController] setTargetRect:self.frame inView:self.superview];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
